const express =require("express")
const md5 =require("md5")
//引入数据库集合
const adminModel = require('../db/model/adminModel')

const addAdminReg = require("../middleware/addAdminReg")

//引入权限控制校验中间件
const isLoginMiddel = require("../middleware/isLoginMiddel")

const route = express.Router()
route.post("/addAdmin",isLoginMiddel,addAdminReg, async (req, res) => {
    const {
        adminName,
        password,
        regTime
    } = req.body

    try {
        //数据库查询是否存在用户
        const findResult = await adminModel.findOne({
            adminName
        })
        if (findResult) {
            return res.send({
                code: 10001,
                msg: "该管理已存在",
                data: null
            })
        }

        //添加管理员信息
        await adminModel.create({
            adminName,
            password: md5(password + "123"),
            regTime,
        })
        res.send({
            code: 10000,
            msg: "添加成功",
            data: null
        })
    } catch (error) {
        console.log("服务器出现异常");
        res.send({
            code: 10002,
            msg: "添加服务器出现异常",
            data: null
        })
    }
})
module.exports=route